﻿using System;
using System.Threading;

namespace c5
{
    // 定义事件参数类
    public class AlarmEventArgs : EventArgs
    {
        public DateTime AlarmTime { get; }

        public AlarmEventArgs(DateTime alarmTime)
        {
            AlarmTime = alarmTime;
        }
    }

    // 定义闹钟类
    public class Clock
    {
        public event EventHandler Tick;
        public event EventHandler<AlarmEventArgs> Alarm;

        private DateTime alarmTime;

        public Clock(DateTime alarmTime)
        {
            this.alarmTime = alarmTime;
        }

        public void Start()
        {
            while (true)
            {
                OnTick();
                if (DateTime.Now >= alarmTime)
                {
                    OnAlarm(new AlarmEventArgs(alarmTime));
                    break;
                }
                Thread.Sleep(1000); // 每秒触发一次Tick事件
            }
        }

        protected virtual void OnTick()
        {
            Tick?.Invoke(this, EventArgs.Empty);
        }

        protected virtual void OnAlarm(AlarmEventArgs e)
        {
            Alarm?.Invoke(this, e);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DateTime alarmTime = DateTime.Now.AddSeconds(10); // 设置闹钟时间为10秒后
            Clock clock = new Clock(alarmTime);

            clock.Tick += (sender, e) =>
            {
                Console.WriteLine("Tick: " + DateTime.Now);
            };

            clock.Alarm += (sender, e) =>
            {
                Console.WriteLine("Alarm: " + e.AlarmTime);
            };

            clock.Start();
        }
    }
}